/*******************************************************************************
 * Package: com.hngtrust.iir.company.controller
 * Type:    FinanceController
 * Date:    2021-3-31 9:25
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.company.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.iir.company.controller.field.AssetDebtField;
import com.hngtrust.iir.company.controller.field.CashFlowField;
import com.hngtrust.iir.company.controller.field.ProfitField;
import com.hngtrust.iir.company.controller.vo.ListAssetDebtReqVO;
import com.hngtrust.iir.company.controller.vo.ListAssetDebtRespVO;
import com.hngtrust.iir.company.controller.vo.ListAssetStructRespVO;
import com.hngtrust.iir.company.controller.vo.ListCashFlowRespVO;
import com.hngtrust.iir.company.controller.vo.ListCashStructRespVO;
import com.hngtrust.iir.company.controller.vo.ListHolderReqVO;
import com.hngtrust.iir.company.controller.vo.ListProfitRespVO;
import com.hngtrust.iir.company.controller.vo.ListProfitStructRespVO;
import com.hngtrust.iir.company.service.FinanceService;
import com.hngtrust.iir.exception.IirErrorType;
import com.hngtrust.iir.exception.IirException;
import com.hngtrust.iir.risk.service.ExcelDownloadService;
import com.tiger.web.annotation.DownloadMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 公司财务信息
 * @author Chentianyong
 * @date 2021-3-31 9:25
 */
@Slf4j
@Api(tags = "公司财务信息")
@RestController
@RequestMapping("/company")
public class FinanceController {
    
    @Autowired
    private FinanceService financeService;
    
    @Autowired
    private ExcelDownloadService excelDownloadSevice;
    
    /**
     * 公司资产负债表
     */
    @ApiOperation(value = "公司资产负债表", notes = "公司资产负债表")
    @PostMapping("/list-asset-debt")
    public ResponseInfo<List<ListAssetDebtRespVO>> listAssetDebt(
            @ApiParam(value = "公司资产负债表") @Valid @RequestBody ListAssetDebtReqVO req) {
        log.info("FinanceController.listAssetDebt start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        List<ListAssetDebtRespVO> resp = financeService.listAssetDebt(req);
    
        ResponseInfo<List<ListAssetDebtRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }

    /**
     * 财务信息结构分析导出
     */
    @ApiOperation(value = "财务信息结构分析导出", notes = "财务信息结构分析导出")
    @DownloadMapping("/export-fina-analy")
    public void exportFinaAnaly(@ApiParam("财务信息结构分析导出") @Valid ListAssetDebtReqVO req, HttpServletResponse response)
            throws Exception {
        log.info("FinanceController.exportAssetDebt start...");
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }

        List<ListAssetDebtRespVO> resp = financeService.listFinaAnaly(req);
        List<Object> result = financeService.changeFinanceData(resp,33,AssetDebtField.values());

        excelDownloadSevice.excelDownload(result, response, "财务信息结构分析表",false);
    }

    /**
     * 财务信息结构分析
     */
    @ApiOperation(value = "财务信息结构分析", notes = "财务信息结构分析")
    @PostMapping("/list-fina-analy")
    public ResponseInfo<List<ListAssetDebtRespVO>> listFinaAnaly(
            @ApiParam(value = "财务信息结构分析") @Valid @RequestBody ListAssetDebtReqVO req) {
        log.info("FinanceController.listAssetDebt start...");

        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }

        List<ListAssetDebtRespVO> resp = financeService.listFinaAnaly(req);

        ResponseInfo<List<ListAssetDebtRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 公司资产负债表10年走势图
     */
    @ApiOperation(value = "公司资产负债表10年走势图", notes = "公司资产负债表10年走势图")
    @PostMapping("/list-asset-debt-trend")
    public ResponseInfo<List<ListAssetDebtRespVO>> listAssetDebtTrend(
            @ApiParam(value = "公司资产负债表") @Valid @RequestBody ListAssetDebtReqVO req) {
        log.info("FinanceController.listAssetDebtTrend start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        List<ListAssetDebtRespVO> resp = financeService.listAssetDebtTrend(req);
        
        ResponseInfo<List<ListAssetDebtRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 公司利润表
     */
    @ApiOperation(value = "公司利润表", notes = "公司利润表")
    @PostMapping("/list-profit")
    public ResponseInfo<List<ListProfitRespVO>> listProfit(
            @ApiParam(value = "公司利润表") @Valid @RequestBody ListAssetDebtReqVO req) {
        log.info("FinanceController.listProfit start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        List<ListProfitRespVO> resp = financeService.listProfit(req);
        
        ResponseInfo<List<ListProfitRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 公司利润表10年走势图
     */
    @ApiOperation(value = "公司利润表10年走势图", notes = "公司利润表10年走势图")
    @PostMapping("/list-profit-trend")
    public ResponseInfo<List<ListProfitRespVO>> listProfitTrend(
            @ApiParam(value = "公司利润表10年走势图") @Valid @RequestBody ListAssetDebtReqVO req) {
        log.info("FinanceController.listProfitTrend start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        
        List<ListProfitRespVO> resp = financeService.listProfitTrend(req);
        
        ResponseInfo<List<ListProfitRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 公司现金流量表
     */
    @ApiOperation(value = "公司现金流量表", notes = "公司现金流量表")
    @PostMapping("/list-cash-flow")
    public ResponseInfo<List<ListCashFlowRespVO>> listCashFlow(
            @ApiParam(value = "公司现金流量表") @Valid @RequestBody ListAssetDebtReqVO req) {
        log.info("FinanceController.listCashFlow start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        List<ListCashFlowRespVO> resp = financeService.listCashFlow(req);
        
        ResponseInfo<List<ListCashFlowRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 公司现金流量表10年走势图
     */
    @ApiOperation(value = "公司现金流量表10年走势图", notes = "公司现金流量表10年走势图")
    @PostMapping("/list-cash-flow-trend")
    public ResponseInfo<List<ListCashFlowRespVO>> listCashFlowTrend(
            @ApiParam(value = "公司现金流量表10年走势图") @Valid @RequestBody ListAssetDebtReqVO req) {
        log.info("FinanceController.listCashFlowTrend start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        List<ListCashFlowRespVO> resp = financeService.listCashFlowTrend(req);
        
        ResponseInfo<List<ListCashFlowRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 公司负债表结构分析
     */
    @ApiOperation(value = "公司负债表结构分析", notes = "公司负债表结构分析")
    @PostMapping("/list-asset-struct")
    public ResponseInfo<ListAssetStructRespVO> listAssetStruct(
            @ApiParam(value = "公司负债表结构分析") @Valid @RequestBody ListHolderReqVO req) {
        log.info("FinanceController.listAssetStruct start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        ListAssetStructRespVO resp = financeService.listAssetStruct(req);
        
        ResponseInfo<ListAssetStructRespVO> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    
    /**
     * 利润表结构分析
     */
    @ApiOperation(value = "利润表结构分析", notes = "利润表结构分析")
    @PostMapping("/list-profit-struct")
    public ResponseInfo<ListProfitStructRespVO> listProfitStruct(
            @ApiParam(value = "利润表结构分析") @Valid @RequestBody ListHolderReqVO req) {
        log.info("FinanceController.listProfitStruct start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        ListProfitStructRespVO resp = financeService.listProfitStruct(req);
        
        ResponseInfo<ListProfitStructRespVO> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 现金流量表结构分析
     */
    @ApiOperation(value = "现金流量表结构分析", notes = "现金流量表结构分析")
    @PostMapping("/list-cash-struct")
    public ResponseInfo<ListCashStructRespVO> listCashStruct(
            @ApiParam(value = "现金流量表结构分析") @Valid @RequestBody ListHolderReqVO req) {
        log.info("FinanceController.listCashStruct start...");
        
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        ListCashStructRespVO resp = financeService.listCashStruct(req);
        
        ResponseInfo<ListCashStructRespVO> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 资产负债表导出
     */
    @ApiOperation(value = "资产负债表导出", notes = "资产负债表导出")
    @DownloadMapping("/export-asset-debt")
    public void exportAssetDebt(@ApiParam("资产负债表导出请求") @Valid ListAssetDebtReqVO req, HttpServletResponse response)
            throws Exception {
        log.info("FinanceController.exportAssetDebt start...");
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
    
        List<ListAssetDebtRespVO> resp = financeService.listAssetDebt(req);
        List<Object> result = financeService.changeFinanceData(resp,req.getType(),AssetDebtField.values());

        excelDownloadSevice.excelDownload(result, response, "资产负债表",false);
    }
    
    /**
     * 利润表导出
     */
    @ApiOperation(value = "利润表导出", notes = "利润表导出")
    @DownloadMapping("/export-profit")
    public void exportProfit(@ApiParam("利润表导出请求") @Valid ListAssetDebtReqVO req, HttpServletResponse response)
            throws Exception {
        log.info("FinanceController.exportProfit start...");
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        
        List<ListProfitRespVO> resp = financeService.listProfit(req);
        List<Object> result = financeService.changeFinanceData(resp,req.getType(), ProfitField.values());

        excelDownloadSevice.excelDownload(result, response, "公司利润表",false);
    }
    
    /**
     * 现金流量表导出
     */
    @ApiOperation(value = "现金流量表导出", notes = "现金流量表导出")
    @DownloadMapping("/export-cash-flow")
    public void exportCashFlow(@ApiParam("现金流量表导出请求") @Valid ListAssetDebtReqVO req, HttpServletResponse response)
            throws Exception {
        log.info("FinanceController.exportCashFlow start...");
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        
        List<ListCashFlowRespVO> resp = financeService.listCashFlow(req);
        List<Object> result = financeService.changeFinanceData(resp,req.getType(), CashFlowField.values());
        
        excelDownloadSevice.excelDownload(result, response, "现金流量表",false);
    }
    
}
